﻿using INTEX.DataBase;
using System;
using System.Data;
namespace WPFExcelReport
{

    public partial class MainViewModel
    {
        public DataSet PlanAB55(DataSet SetEnv, DataSet db, DataRow dwABA, DataRow dwRule)
        {
            DataSet SReturn = new DataSet();
            DataSet SResult = new DataSet();
            DataTable tmpABPOS = INTEXDB.ABPOS.Clone();
            string filter = "";
            string ENDD = "";
            string STARTD = "";
            DataRow[] rows = null;
            DataRow dwART = null;
            string ART = "";
            string VAR = "";
            string WAKZ = "";
            string FARBE = "";
            string BART = "";
            string BVAR = "";
            string BFARBE = "";
            DataRow dwAPZUORD = null;
            string APZANRKRS = "";
            string APZALFDNR = "";
            DataRow dwABPN = null;
            DataRow dwMU = null;
            double ROLLS = 1;
            #region========Validate=================
            string LFD0 = dwABA["ABAAUFLFD"].ToString();
            if (LFD0.Trim() == "")
                LFD0 = ABPLFD;
            string POS0 = dwABA["ABAAUFPOS"].ToString();
            if (POS0.Trim() == "")
            {
                POS0 = ABPOS.ToString(); ABPOS++;
            }
            ART = dwABA["ABAART"].ToString();
            VAR = dwABA["ABAVAR"].ToString();
            WAKZ = dwABA["ABAWAKZ"].ToString();
            FARBE = dwABA["ABAFARBE"].ToString(); 
            
            filter = "WAART='" + ART + "' AND WAVAR='" + VAR + "'";
            rows = db.Tables["WASTAMM"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwART = row;
                break;
            }
            if (dwART == null)
            {
                throw new System.Exception("没有对应的工艺档案");
                return SReturn;
            }

            filter = "APZSTUFE='R' AND APZART='" + ART
                + "' AND APZVAR='" + VAR + "' ";
            rows = db.Tables["APZUORD"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwAPZUORD = row;
                break;
            }
            if (dwAPZUORD == null)
            {
                //throw new System.Exception("没有对应的工艺流程");
                //return SReturn;
                APZANRKRS = "WV";
                APZALFDNR = "0001";
            }
            else
            {
                APZANRKRS = dwAPZUORD["APZANRKRS"].ToString();
                APZALFDNR = dwAPZUORD["APZALFDNR"].ToString();
            }
            DataRow dwWASTKE = null;
            DataRow dwBSGROE = null;
            filter = "WAKART='" + ART + "' AND WAKVAR='" + VAR + "' AND WAKWCOL='" + FARBE + "'";
            rows = db.Tables["WASTKE"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwWASTKE = row;
                break;
            }
            if (dwWASTKE == null)
            {
                throw new System.Exception("没有对应的织轴工艺档案");
                return SReturn;
            }
           
            BART = dwWASTKE["WAKKART"].ToString();
            BVAR = dwWASTKE["WAKKVAR"].ToString();
            BFARBE = dwWASTKE["WAKKFARBE"].ToString();
            filter = "BSGART='" + BART + "'";
            rows = db.Tables["BSGROE"].Select(filter);
            foreach (DataRow row in rows)
            {
                dwBSGROE = row;
                break;
            }

            filter = "BSKART='" + BART + "'";
            rows = db.Tables["BSSATZ"].Select(filter);
            DataRow dwBSSATZ = null;
            foreach (DataRow row in rows)
            {
                dwBSSATZ = row;
                break;
            }
   
            filter = "APFANRKRS='" + APZANRKRS + "' AND APFALFDNR='" + APZALFDNR + "'";
            rows = db.Tables["APFOLGE"].Select(filter);
            foreach (DataRow row in rows)
            {
                PRLO = row["KSZLO"].ToString();
                PRLB = row["KSZLB"].ToString();
                PRLP = row["KSZLP"].ToString();
                break;
            }
            double WAFBRTL = System.Convert.ToDouble(dwART["WAFBRTL"].ToString());
            double WARGRLFD = System.Convert.ToDouble(dwART["WARGRLFD"].ToString());
            double WAFGRLFD = System.Convert.ToDouble(dwART["WAFGRLFD"].ToString());
            double WARSTDLG = System.Convert.ToDouble(dwART["WARSTDLG"].ToString());
            double WARWBRT = System.Convert.ToDouble(dwART["WARWBRT"].ToString());
            if (WARSTDLG == 0)
            {
                WARSTDLG = 100000;
            }
            #endregion
            #region======Rules======== 
            QTYEND = 0; QTYMAT = 0; R01 = 0; R02 = 0; F01 = 0; F02 = 0; JNUM = 0; JWASTGE = 0;
            if (dwRule != null)
            {
                R01 = Convert.ToDouble(dwRule["R01"].ToString()) / 100;
                F01 = Convert.ToDouble(dwRule["F01"].ToString());
                F02 = Convert.ToDouble(dwRule["F02"].ToString());
                LDAB = Convert.ToDouble(dwRule["LEADTIME"].ToString());//生产周期
                JWASTGE = Convert.ToDouble(dwRule["JWASTGE"].ToString());
                DELDATE = dwRule["DELDATE"].ToString();
                QTYEND =Convert.ToDouble(dwRule["QTYEND"].ToString());
                JNUM = Convert.ToDouble(dwRule["JNUM"].ToString());
            }
            if (QTYEND < 0)
            {
                QTYEND = (Convert.ToDouble(dwABA["ABARESMG"].ToString()) / 10) * 0.01;//无放大系数
            }
            else
            {
                QTYEND = QTYEND;
            }
            ROLLS = Math.Ceiling(0.01*QTYEND*100 / (WARSTDLG * 0.001));//无放大系数
             
            if (DELDATE.ToString().Trim().Length<8)
            {
                ENDD = dwABA["ABAVKSTARTD"].ToString();
            }
            else
            {
                ENDD = DELDATE.ToString();
            }

            if (ENDD == "0")
            {
                ENDD = date;
            }
            STARTD = Helpers.Helper.GetDateAddDays(ENDD, Convert.ToInt16(0 - LDAB));
            if (STARTD.Trim() == "") STARTD = "0";
            #endregion
        
            #region=======dwABPN==========
            dwABPN = tmpABPOS.NewRow();
            dwABPN["ABPAENDZL"] = 5;
            dwABPN["ABPMANDANT"] = app.Mandant;
            dwABPN["ABPAUFART"] = "PR";
            dwABPN["ABPAUFKRS"] = "T5";
            dwABPN["ABPAUFLFD"] = LFD0;
            dwABPN["ABPAUFPOS"] = POS0;
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date;
            dwABPN["ABPFA"] = 0;
            dwABPN["ABPGA"] = 0;
            dwABPN["ABPQA"] = 0;
            dwABPN["ABPZLO"] = PRLO;
            dwABPN["ABPZLB"] = PRLB;
            //dwABPN["ABPBLO"] = PRLO;
            dwABPN["ABPGESSTA"] = "TE";
            dwABPN["ABPWBSTA"] = "60";
            dwABPN["ABPAESTAT"] = "J";
            dwABPN["ABPSERIE"] = " ";
            dwABPN["ABPKETTAUFT"] = "N";
            dwABPN["ABPMGV"] = 0;
            dwABPN["ABPMGB"] = 0;
            dwABPN["ABPTMCOUNT"] = 3;
            dwABPN["ABPREGEL"] = "00000000";
            dwABPN["ABPLAEAENDA"] = 0;
            dwABPN["ABPWAART"] = dwART["WAWAART"];
            dwABPN["ABPWAKZ"] = "R";
            dwABPN["ABPART"] = ART;
            dwABPN["ABPVAR"] = VAR;
            dwABPN["ABPFARBE"] = FARBE;
            dwABPN["ABPLTVORL"] = dwABA["EXART"];
            dwABPN["ABPANRKR"] = APZANRKRS;
            dwABPN["ABPALFDNR"] = APZALFDNR;
            dwABPN["ABPLIEFDAT"] = ENDD;
            dwABPN["ABPSTARTDAT"] = STARTD;
            dwABPN["ABPAENDDAT"] = dwBSSATZ["BSFDZ"];
            dwABPN["ABPSTDLAE"] = 0;
            dwABPN["ABPDIVARTKZ"] = " ";
            dwABPN["ABPSOLLMENGE1"] = QTYEND*100;//10
            dwABPN["ABPSOLLMENGE2"] = QTYEND*100;
            dwABPN["ABPSOLLMENGE3"] = QTYEND*100 * WARWBRT * 0.001;
            dwABPN["ABPSOLLMENGE4"] = QTYEND * 100 * WAFBRTL * 0.001;
            dwABPN["ABPSOLLMENGE5"] = QTYEND * 100 * WARGRLFD * 0.001;
            dwABPN["ABPSOLLMENGE6"] = QTYEND * 100 * WAFGRLFD * 0.001;
            dwABPN["ABPSOLLMENGE7"] = "0";
            dwABPN["ABPSOLLMENGE8"] = ROLLS * 100;

            dwABPN["ABPANZSTK"] = ROLLS;
            dwABPN["ABPZUGETGES"] = dwABPN["ABPSOLLMENGE1"];
            dwABPN["ABPSTKLG"] = Math.Ceiling(QTYEND * 100 / JNUM);
            dwABPN["ABPSZUSATZKZ1"] = " ";

            dwABPN["ABPSME"] = 1;
            dwABPN["ABPNEUBEN"] = app.User;
            dwABPN["ABPNEUDAT"] = date; ;
            tmpABPOS.Rows.Add(dwABPN);
            #endregion
            #region=======MATERIAL ================
            var query = from DataRow row in tbABAUFM.AsEnumerable()
                        where row["ABAID"].ToString() == dwABA["ABAID"].ToString()
                        select row;
            foreach (DataRow row in query)
            { 
                row["ABAAUFTYP"] = "V";
                row["ABAAUFKRS"] = dwABPN["ABPAUFKRS"];
                row["ABAAUFLFD"] = dwABPN["ABPAUFLFD"];
                row["ABAAUFPOS"] = dwABPN["ABPAUFPOS"];
                row["ABASANZSTK"] = ROLLS;
                row["ABASSTKLG"] = QTYEND*100 / ROLLS;
                row["ABAQTYRESERVE"] = QTYEND*100;
            }
            //产生下层需求
            #region============BAAUF733====================
            double RT = Convert.ToDouble(dwWASTKE["WAKEAFAKTR"].ToString()); 
            if (RT == 0)
            {
                RT = 1000;
            } 
            QTYMAT = Math.Ceiling((QTYEND/ (1 - R01) + JNUM * JWASTGE));//无放大倍数           
            double beamLg = 2500;
            if (JNUM < 0)
            {
                int beamnum = Convert.ToInt16(Math.Ceiling(QTYMAT/beamLg));
                JNUM = beamnum;
            }
            beamLg = Math.Ceiling(QTYMAT / JNUM);
            dwMU = tbABAUFM.NewRow();
            dwMU.ItemArray = NewABA().ItemArray;
            dwMU["ABAID"] = ABAID; ABAID++;
            #region=====MapRules=======
            dwMU["QTYEND"] = dwRule["QTYEND"];
            dwMU["QTYMAT"] = dwRule["QTYMAT"];
            dwMU["DELDATE"] = dwRule["DELDATE"];
            dwMU["STARTDATE"] = dwRule["STARTDATE"];
            dwMU["LEADTIME"] = dwRule["LEADTIME"];
            dwMU["UNITNUM"] = dwRule["UNITNUM"];
            dwMU["TTENDS"] = dwRule["TTENDS"];
            dwMU["UENDS"] = dwRule["UENDS"];
            dwMU["UNITQTY"] = dwRule["UNITQTY"];
            dwMU["R01"] = dwRule["R01"];
            dwMU["R01DES"] = dwRule["R01DES"];
            dwMU["R02"] = dwRule["R02"];
            dwMU["R02DES"] = dwRule["R02DES"];
            dwMU["F01"] = dwRule["F01"];
            dwMU["F01DES"] = dwRule["F01DES"];
            dwMU["F02"] = dwRule["F02"];
            dwMU["F02DES"] = dwRule["F02DES"];
            dwMU["JNUM"] = dwRule["JNUM"];
            dwMU["JWASTGE"] = dwRule["JWASTGE"];
            dwMU["JWASTAGEDES"] = dwRule["JWASTAGEDES"];
            dwMU["UMS"] = dwRule["UMS"];
            #endregion
            //经如何计算而得到的
            //dwMU["JWASTGE"] = JWASTGE;
            dwMU["ABAMANDANT"] = app.Mandant;
            dwMU["ABAAUFTYP"] = "N";
            dwMU["ABAVKAUFTYP"] = "V";
            dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
            dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
            dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
            dwMU["ABAVKUPOS"] = Helpers.Helper.Right("0000" + dwWASTKE["WAKPOS"].ToString(), 2);
            dwMU["ABASA"] = 733;
            dwMU["ABAUMART"] = dwBSGROE["BSGKETTART"];
            dwMU["ABPWAKZ"] = "R";
            dwMU["ABPART"] = ART;
            dwMU["ABPVAR"] = VAR;
            dwMU["ABPFARBE"] = FARBE;

            dwMU["ABAWAKZ"] = "W";
            dwMU["ABAART"] = BART;
            dwMU["ABAVAR"] = BVAR;
            dwMU["ABAFARBE"] = BFARBE;

            dwMU["ABARESMG"] = JNUM * beamLg * 1000;
            dwMU["ABASSTKLG"] = beamLg;
            dwMU["ABASSTKWG"] = beamLg;
            dwMU["ABARESKG"] = beamLg;
            dwMU["ABAREUNIT"] = JNUM;
            dwMU["BMBAUMKRS"] = "SZ";
            dwMU["BMBAUMLFD"] = "123456";
            dwMU["ABABSFDZ"] = dwBSSATZ["BSFDZ"];
            dwMU["EXART"] = dwABA["EXART"];
            dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
            dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];
            dwMU["ABABASMG"] = Convert.ToDecimal(dwWASTKE["WAKEAFAKTR"].ToString()) / 10;
            dwMU["ABADISPOKZ"] = dwWASTKE["WAKDISPOKZ"];
            dwMU["ABALLEIST"] = dwWASTKE["WAKLLEIST"];
            dwMU["ABARLEIST"] = dwWASTKE["WAKRLEIST"];
            dwMU["ABADOMKZ"] = dwWASTKE["WAKSTBR"];
            dwMU["ABALO"] = PRLO;
            dwMU["ABALB"] = PRLB;
            dwMU["ABALP"] = PRLP;
            dwMU["ABAUSER"] = app.User;
            dwMU["ABADATE"] = date;
            tbABAUFM.Rows.Add(dwMU);
            #endregion
            #region============GAAUF733==========================
            double GAKG1 = 0;
            double GABASMG2 = 0; 
            filter = "WAGART='" + ART + "' AND WAGVAR='" + VAR + "' AND WAGWCOL='" + FARBE + "'";
            string sort = "WAGFOLGEPOS ASC";
            rows = db.Tables["WASTGA"].Select(filter,sort);
            double addweight = 0;
            foreach (DataRow dwWASTGA in rows)
            { 
                RT = Convert.ToDouble(dwWASTGA["WAGKGBRUT"]) * 0.001 * 0.01;
                double f02 = Convert.ToDouble(dwWASTGA["WAGABF"].ToString());
                if (f02 > 0)
                    F02 = f02;
                if (RT == 0)
                {
                    throw new Exception("请核对织轴工艺!");
                }
                if (F02 == 0)
                    F02 = 1;
                if (F01 == 0)
                    F01 = F02;
                GAKG1 = Math.Ceiling(QTYEND*RT*F02/F01);  
                dwMU = tbABAUFM.NewRow();
                dwMU["ABAID"] = ABAID; ABAID++;
                #region=====MapRules=======
                dwMU["QTYEND"] = dwRule["QTYEND"];
                dwMU["QTYMAT"] = dwRule["QTYMAT"];
                dwMU["DELDATE"] = dwRule["DELDATE"];
                dwMU["STARTDATE"] = dwRule["STARTDATE"];
                dwMU["LEADTIME"] = dwRule["LEADTIME"];
                dwMU["UNITNUM"] = dwRule["UNITNUM"];
                dwMU["TTENDS"] = dwRule["TTENDS"];
                dwMU["UENDS"] = dwRule["UENDS"];
                dwMU["UNITQTY"] = dwRule["UNITQTY"];
                dwMU["R01"] = dwRule["R01"];
                dwMU["R01DES"] = dwRule["R01DES"];
                dwMU["R02"] = dwRule["R02"];
                dwMU["R02DES"] = dwRule["R02DES"];
                dwMU["F01"] = dwRule["F01"];
                dwMU["F01DES"] = dwRule["F01DES"];
                dwMU["F02"] = F02;// dwRule["F02"];
                dwMU["F02DES"] = dwRule["F02DES"];
                dwMU["JNUM"] = dwRule["JNUM"];
                dwMU["JWASTGE"] = dwRule["JWASTGE"];
                dwMU["JWASTAGEDES"] = dwRule["JWASTAGEDES"];
                dwMU["UMS"] = dwRule["UMS"];
                #endregion

                dwMU["ABAMANDANT"] = app.Mandant;
                dwMU["ABAAUFTYP"] = "N";
                dwMU["ABAVKAUFTYP"] = "V";
                dwMU["ABAVKAUFKRS"] = dwABPN["ABPAUFKRS"];
                dwMU["ABAVKAUFLFD"] = dwABPN["ABPAUFLFD"];
                dwMU["ABAVKAUFPOS"] = dwABPN["ABPAUFPOS"];
                dwMU["ABAVKUPOS"] = Helpers.Helper.Right("00"+dwWASTGA["WAGFOLGEPOS"].ToString(),2);
                dwMU["ABASA"] = 733;
                dwMU["ABPWAKZ"] = "R";
                dwMU["ABPART"] = ART;
                dwMU["ABPVAR"] = VAR;
                dwMU["ABPFARBE"] = FARBE;
                dwMU["ABACOLORREF"] = dwWASTGA["WAGBEM"]; ;
                
                dwMU["EXART"] = dwABA["EXART"];
                dwMU["ABAAUFMSL"] = "  ";
                dwMU["ABAPACKTYP"] = "    ";
                dwMU["ABAHUELSE"] = "      ";
                dwMU["ABAWAKZ"] = "G";
                dwMU["ABAART"] = dwWASTGA["WAGGART"];
                dwMU["ABAVAR"] = dwWASTGA["WAGGVAR"];
                dwMU["ABAFARBE"] = dwWASTGA["WAGGFARBE"];
                dwMU["ABASSTKLG"] = 0;
                dwMU["ABARESMG"] = GAKG1 * 1000;
                dwMU["ABASANZSTK"] = Math.Ceiling(GAKG1);
                dwMU["ABASSTKGEW"] = "1000";
                dwMU["ABAQTYALLOCATE"] = 0;
                dwMU["ABAVKENDD"] = dwABPN["ABPLIEFDAT"];
                dwMU["ABAVKSTARTD"] = dwABPN["ABPSTARTDAT"];
                dwMU["ABABASMG"] = dwWASTGA["WAGKGNET"];
                dwMU["ABADISPOKZ"] = dwWASTGA["WAGDISPOKZ"];
                dwMU["ABALO"] = PRLO;
                dwMU["ABALB"] = PRLB;
                dwMU["ABALP"] = PRLP;
                dwMU["ABAUSER"] = app.User;
                dwMU["ABADATE"] = date;
                tbABAUFM.Rows.Add(dwMU);
            }
            #endregion
            #endregion
            SReturn.Merge(tmpABPOS);
            return SReturn;
        }
    }
}